import { createRouter, createWebHistory } from "vue-router";

const isAuthenticated = localStorage.getItem("user")
    ? JSON.parse(localStorage.getItem("user")).loginauth
    : null;
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: "/",
            name: "index",
            component: () => import("../views/Index.vue"),
        },
        // {
        //     path: "/",
        //     redirect: "/index",
        // },
        {
            path: "/login",
            name: "login",
            component: () => import("../views/Login.vue"),
            meta: {
                noTab: true,
            },
        },
        {
            path: "/myinfo",
            name: "myinfo",
            component: () => import("../views/MyInfo.vue"),
        },
        {
            path: "/suggestion",
            name: "suggestion",
            component: () => import("../views/Suggestion.vue"),
        },
        {
            path: "/mine",
            name: "mine",
            component: () => import("../views/Mine.vue"),
        },
        {
            path: "/myactivity",
            name: "myactivity",
            component: () => import("../views/MyActivity.vue"),
        },
        {
            path: "/mydetail",
            name: "mydetail",
            component: () => import("../views/MyDetail.vue"),
        },
        // {
        //     path: "/nf",
        //     name: "nf",
        //     component: () => import("../views/Nf.vue"),
        // },
        {
            path: "/activity",
            name: "activity",
            component: () => import("../views/Activity.vue"),
        },
        {
            path: "/recruit",
            name: "recruit",
            component: () => import("../views/Recruit.vue"),
        },
        {
            path: "/bored",
            name: "bored",
            component: () => import("../views/Bored.vue"),
        },
        {
            path: "/:catchAll(.*)", //匹配未定义的路由
            redirect: "/login", //重定向
        },
    ],
});
function isPhone() {
    return /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent);
}
router.beforeEach((to, from, next) => {
    if (to.path) {
        if (window._hmt) {
            window._hmt.push(["_trackPageview", "/#" + to.fullPath]);
        }
    }
    // if (to.name === "down") {
    //     next();
    // } else {
    // if (!isPhone()) {
    //     if (to.name === "nf") {
    //         next();
    //     } else {
    //         next({ name: "nf" });
    //     }
    // } else {
    // if (to.name === "nf") {
    //     next({ name: "index" });
    // } else {
    if (
        // to.name !== "index" &&
        to.name !== "login" &&
        !isAuthenticated
    ) {
        // next({ name: "index" });
        next({ name: "login" });
    } else {
        next();
    }
    // }
    // }
    // }
});

export default router;
